package com.jdk.learn.thread;

import org.redisson.api.RLock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by baiguantao on 2017/10/10.
 * 多次进来是一样的
 *
 */
public class SynchronizedDemo {
    public static void main(String[] args) throws Exception {
        ExecutorService executorService= Executors.newFixedThreadPool(4);
        SynchronizedDemo synchronizedDemo=new SynchronizedDemo();
        for (int i=0;i<100;i++){
        executorService.execute(new Runnable(){

            @Override
            public void run() {
                String name="";
                try {
                    Thread.sleep(100);
                    name="ricky";
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronizedDemo.backMoney(name);
            }
        });
        }
        executorService.shutdown();
    }

    private int i=0;
    /**
     * 模拟还款
     */
    public void backMoney(String name){
        RLock rLock=DLock.acquireLock(name);
        try {
//            Thread.sleep(100);
            System.out.println("i:"+i);
            i++;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DLock.realeaseLock(rLock);
        }


    }
}
